package org.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.Product;
import org.csu.mypetstore.api.persistence.ProductMapper;
import org.csu.mypetstore.api.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("productService")
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public CommonResponse<List<Product>> getProductListByCategory(String categoryId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", categoryId);
        List<Product> productList = productMapper.selectList(queryWrapper);
        if (productList.size() == 0) {
            return CommonResponse.createForSuccessMessage("No such info");
        }
        return CommonResponse.createForSuccess(productList);
    }

    @Override
    public CommonResponse<Product> getProductById(String productId) {
        Product product = productMapper.selectById(productId);
        if (product == null) {
            return CommonResponse.createForSuccessMessage("No such product with the id given");
        }
        return CommonResponse.createForSuccess(product);
    }

    @Override
    public CommonResponse<List<Product>> getAllProductList() {
        List<Product> productList = productMapper.selectList(null);
        if (productList.size() == 0) {
            return CommonResponse.createForSuccessMessage("No such info");
        }
        return CommonResponse.createForSuccess(productList);
    }

    @Override
    public CommonResponse<List<Product>> searchProductList(String keyword) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("productid", "%"+keyword.toLowerCase()+"%");
        List<Product> productList = productMapper.selectList(queryWrapper);
        if (productList.size() == 0) {
            return CommonResponse.createForSuccessMessage("No such info");
        }
        return CommonResponse.createForSuccess(productList);
    }
}